package org.NooLab.itexx.comm.messages;

import org.NooLab.itexx.comm.tcp.box.infra.DataSenderIntf;
import org.NooLab.itexx.comm.util.XmlStringHandling;
// import org.NooLab.structures.AppInstanceControlIntf;
import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.logging.PrintLog;
import org.NooLab.utilities.strings.StringsUtil;



/**
 * 
 * this class provides a TcpBox for exchanging messages within the population of iTexx applications;
 * 
 *  it also provides a decoder for the messages, which come in simple XML. either through the console, 
 *  or through the TcpBox
 *  
 *  Actions itself, even system.exit(), will be called in the hosting application through callbacks
 * 
 * 
 *
 */
public class CommBoxMessage implements CommBoxMessageIntf{

	// AppInstanceControlIntf app;
	int issuer = -1;
	
	DFutils fileutil = new DFutils();
	PrintLog out = new PrintLog(2,false);

	private String originatorStr="";
	// ========================================================================
	public CommBoxMessage(  int issuer){
		this.issuer = issuer;
		 
	}
	// ========================================================================
	
	public CommBoxMessage setOriginator( String originatorStr ){
		
		this.originatorStr  = " "+originatorStr + " ";
		return this;
	}
	
	public void issueConsoleMessage( int msgId, String processName, String secret, int clientPort ){
		
		String xformedMsg;
		
		xformedMsg = composeMessage( msgId, processName, secret, clientPort);
		
		// "@itexx :: <action name=\"stop\"/><process name=\"\" secret=\"\"/>");
		out.print(1, xformedMsg );
		
		if (msgId == CommBoxMessageIntf._MSG_STOPPING){
			 
		}
		
	}

	public void issueTcpMessage( DataSenderIntf sender, int msgId, String processName, String secret ) throws Exception{
		issueTcpMessage( sender, 0, msgId, processName, secret,0);
	}
	
	public int issueTcpMessage( DataSenderIntf sender, int clientPort, int msgId, String processName, String secret,int code  ) throws Exception{
		
		int result= -1;
		String xformedMsg;
		
		if (sender==null){
			return -2;
		}
		xformedMsg = composeMessage( msgId, processName, secret,code,clientPort);
		
		
		if (msgId == CommBoxMessageIntf._MSG_STARTUP_COMPLETION){
			
			result = sender.send( xformedMsg );	 
		}
		
		if (msgId == CommBoxMessageIntf._MSG_STOPPING){
			 
		}
		return result ;
	}

	/**
	 * 
	 * only the iTexx framework application is allowed to clear the stopping files via the CommBox;
	 * yet, the applications clear it on startup, too, by a private process 
	 * 
	 * @param msgId
	 * @param filepath
	 * @param processName
	 * @param secret
	 */
	public void clearStoppingFile( int msgId, String filepath, String processName, String secret ){
		
		boolean rB=false; 
		String filename;
		
		
		if (issuer != CommBoxMessageIntf._MSG_ISSUER_ITEXX){
			return;
		}
		
		try{

			filename = DFutils.createPath(filepath, "stop") ;
			
			rB = DFutils.fileExists(filename)  ;
							
			if (rB ){
				
				// check for contents?
				fileutil.deleteFile(filename) ;
			}
			
			
		}catch(Exception e){
			
		}
		
		
	}
	
	
	public boolean issueStoppingFile( int msgId, String filepath, String processName, String secret ){
		
		boolean rB=false; 
		if ((issuer == CommBoxMessageIntf._MSG_ISSUER_ITEXX) && (msgId == CommBoxMessageIntf._MSG_STOPPING)){
			
			
			String filename;
			 
			try{

				clearStoppingFile( msgId, filepath, processName, secret);
				
				filename = DFutils.createPath(filepath, "stop") ;
				
				
				
				
			}catch(Exception e){
				
			}
			
			
		}
		return rB;
	}
	
	// ----------------------------------------------------
	/**
	 * 
	 * note that the process name and the secret (a guid) has been issued by the iTexx framework;
	 * it has been passed through the CLI to the application instance
	 * 
	 */
	private String composeMessage(int msgId, String processName, String secret, int clientPort ){
		return composeMessage( msgId, processName, secret,0 ,clientPort);
	}
	
	private String composeMessage(int msgId, String processName, String secret, int code, int clientPort){	
		String msgpart="",xformedMsg="";
		
		if (msgId == CommBoxMessageIntf._MSG_STOPPING){
			msgpart = getStoppingMessage() ;
		}
		
		if (msgId == CommBoxMessageIntf._MSG_STARTUP_COMPLETION){
			msgpart = getStartupMessage(code) ;
		}
		xformedMsg = "@iTexx :: "+msgpart+ "<process name=\""+processName+"\" secret=\""+secret+"\"  responseport=\""+clientPort+"\" />";
		// message will look like this:  "@itexx :: <action name="stop"/><process name="" secret="" />");
											// out.printErr(4, "formed message : \n"+xformedMsg);		
		return xformedMsg;
	}
	
	
	private String getStartupMessage(int code) {
		// 
		String msg ="";
		
		if (code==0){
			msg = "<action name=\"startup\" value=\"completed\" code=\"0\" />";
		}else{
			msg = "<action name=\"startup\" value=\"failed\" code=\""+code+"\" />";
		}
		
		return msg;
	}
	
	private String getStoppingMessage() {

		String msg ="";
		
		if (issuer == CommBoxMessageIntf._MSG_ISSUER_ITEXX){
			msg = "stop";
		}
		if (issuer == CommBoxMessageIntf._MSG_ISSUER_APP){
			msg = "stopping";
		}
		msg = "<action name=\""+msg+"\"/>";
		
		return msg;
	}
			
}

// serves as a fast compartment fork for the stream of messages
class Interpreter{
	
	XmlStringHandling xMsg = new XmlStringHandling();
	
	// ----------------------------------------------------
	public Interpreter(){
		
	}
	// ----------------------------------------------------
	
	
	
	
}
